package com.ble.utlis;

import android.util.Log;

import com.ble.bean.CmdEntity;

public class CmdUtlis {

    public static CmdEntity getData(byte[] da) {
//        boolean b = CrcUtlis.checkCrc(da);
//        if (!b) {
//            return null;
//        }
        int i = TextTools.byteToInt1(da[2]);
        byte[] data = new byte[i];
        System.arraycopy(da, 3, data, 0, data.length);

        int type=da[1];
        CmdEntity cmdEntity = new CmdEntity(type,0, TextTools.byteToInt2(data),data);

        return cmdEntity;
    }



    public static byte slaveAddress = 0x01;

    public static byte[] grtCmd(byte code, int adress, int count) {
        byte[] re = new byte[6];
        int index = 0;
        re[index++] = slaveAddress;
        re[index++] = code;
        byte[] b_adress = TextTools.intToByte2(adress);
        System.arraycopy(b_adress, 0, re, index, b_adress.length);
        index += b_adress.length;
        byte[] b_count = TextTools.intToByte2(count);
        System.arraycopy(b_count, 0, re, index, b_count.length);
        index += b_count.length;
        re = crcCombination(re);
        return re;
    }

    public static byte[] grtCmdSetting(byte code, int adress, int count, byte[] date) {
        byte[] re = new byte[6 + date.length + (date.length!=0?2:0)];
        int index = 0;
        re[index++] = slaveAddress;
        re[index++] = code;
        byte[] b_adress = TextTools.intToByte2(adress);
        System.arraycopy(b_adress, 0, re, index, b_adress.length);
        index += b_adress.length;
        byte[] b_count = TextTools.intToByte2(count);
        System.arraycopy(b_count, 0, re, index, b_count.length);
        index += b_count.length;
        if(date.length!=0) {
            byte[] b_length = TextTools.intToByte2(date.length);
            System.arraycopy(b_length, 0, re, index, b_length.length);
            index += b_length.length;
            System.arraycopy(date, 0, re, index, date.length);
            index += date.length;
        }
        re = crcCombination(re);

        return re;
    }

    public static byte[] grtCmdSetting2(byte code, int adress,   byte[] date) {
        byte[] re = new byte[4 + date.length];
        int index = 0;
        re[index++] = slaveAddress;
        re[index++] = code;
        byte[] b_adress = TextTools.intToByte2(adress);
        Log.e("TAG", "grtCmdSetting2: "+TextTools.byteToHexString(b_adress) +"    "+adress);
        System.arraycopy(b_adress, 0, re, index, b_adress.length);
        index += b_adress.length;
        //byte[] b_length = TextTools.intToByte2(date.length);
        //System.arraycopy(b_length, 0, re, index, b_length.length);
        //index += b_length.length;
        System.arraycopy(date, 0, re, index, date.length);
        index += date.length;
        re = crcCombination(re);
        return re;
    }

    private static byte[] crcCombination(byte[] date) {
        byte[] re = new byte[date.length + 2];
        System.arraycopy(date, 0, re, 0, date.length);
        byte[] crc = CrcUtlis.getcrc(date);
        System.arraycopy(crc, 0, re, date.length, crc.length);
        return re;
    }


}
